package com.mcxx.modules.formulcount;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.common.exception.BizException;
import com.mcxx.formulcount.gonshi.BlockServer;
import com.mcxx.formulcount.gonshi.FormulaConvert;
import com.mcxx.formulcount.gonshi.ISentence;
import com.mcxx.formulcount.gonshi.RefObject;
import com.mcxx.formulcount.param.FormulCountParam;
import com.mcxx.formulcount.param.SecurityParam;
import com.mcxx.constant.DictConstant;
import com.mcxx.modules.base.service.read.DictionaryReadService;
import com.mcxx.modules.miniSalvation.order.entity.StandardEntity;
import com.mcxx.modules.system.service.read.SalvationRuleReadService;
import com.mcxx.modules.miniSalvation.order.service.read.StandardReadService;
import com.mcxx.modules.system.entity.SalvationRuleEntity;
import com.mcxx.result.BaseResult;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.module.dict.entity.TaDictPo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


@Service("formulCountService")
public class FormulCountService {
    private final static Logger logger = LoggerFactory.getLogger(FormulCountService.class);
    @Autowired
    private SalvationRuleReadService salvationRuleReadService;
    @Autowired
    private StandardReadService standardReadService;
    @Autowired
    private DictionaryReadService dictionaryReadService;


    public BaseResult<Map<String, Object>> salvationFormulCount(FormulCountParam param) {
        BaseResult<Map<String, Object>> result = new BaseResult<>();
        Map<String, Object> map = new HashMap<>();
        if (CommonUtil.invalidArgs(param.getAreaCode(), param.getBusinessType(), param.getTaOrgId(),
                param.getFormulaValue(), param.getTypeName())) {
            result.setCode(FailureCode.ERR_10000.getCode());
            result.setMsg(FailureCode.ERR_10000.getMessage());
            return result;
        }
        if (param.getTypeName().equals("手工填写金额")) {
            return result;
        }
        param.setAreaCode(param.getAreaCode());
        param.setBusinessType(param.getBusinessType());
        param.setTaOrgId(param.getTaOrgId());
        try {
            if (param.getTypeName().indexOf("实物救助") != -1) {
                List<SalvationRuleEntity> list = salvationRuleReadService.queryRuleCount(param);
                IdentityHashMap<String, Double> jgMap = new IdentityHashMap<>();
                IdentityHashMap<String, Double> pxMap = new IdentityHashMap<>();

                if (StringUtils.isNotBlank(param.getKindValue())) {
                    String[] strKin = param.getKindValue().split(",");
                    for (int i = 0; i < strKin.length; i++) {
                        String id = strKin[i];
                        param.setKindValue(id);
                        if (null != list && list.size() > 0) {
                            for (SalvationRuleEntity entity : list) {
                                if (StringUtils.isNotBlank(entity.getRelationshipType()) && entity.getRelationshipType().equals("01")) {

                                    Hashtable<String, Object> varValues = putValue(param);
                                    String str = entity.getChineseFormula();
                                    if (str.indexOf("实物种类") != -1) {

                                        FormulaConvert cvt = new FormulaConvert();
                                        ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                                        RefObject rnt = new RefObject(0);
                                        ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                                        Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                                        for (String k : rnts.keySet()) {
                                            //System.out.println(k + "=" + rnts.get(k));
                                            jgMap.put(k, (Double) rnts.get(k));
                                        }
                                    }
                                } else {
                                    Hashtable<String, Object> varValues = putValue(param);
                                    String str = entity.getChineseFormula();
                                    if (str.indexOf("实物种类") != -1) {

                                        FormulaConvert cvt = new FormulaConvert();
                                        ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                                        RefObject rnt = new RefObject(0);
                                        ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);
                                        Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                                        for (String k : rnts.keySet()) {
                                            //System.out.println(k + "=" + rnts.get(k));
                                            pxMap.put(k, (Double) rnts.get(k));
                                        }
                                    }
                                }
                            }
                        } else {
                            result.setCode("400");
                            result.setMsg("规则为空，请先配置规则");
                            logger.error("规则为空，请先配置规则, param={}", param);
                            return result;
                        }

                    }
                }
                //System.out.println(jgMap);
                //System.out.println(pxMap);
                if (null != jgMap && jgMap.size() > 0) {
                    List<Double> dlist = new ArrayList<>();
                    for (String k : jgMap.keySet()) {
                        if (!dlist.contains(jgMap.get(k))) {
                            dlist.add(jgMap.get(k));
                        }
                    }
                    Double max = Collections.max(dlist);
                    map.put("money", max);
                } else if (null != pxMap && pxMap.size() > 0) {
                    List<Double> dlist = new ArrayList<>();
                    for (String k : pxMap.keySet()) {
                        dlist.add(pxMap.get(k));
                    }
                    Double sum = 0d;
                    for (Double dd : dlist) {
                        BigDecimal bd1 = new BigDecimal(Double.toString(sum));
                        BigDecimal bd2 = new BigDecimal(Double.toString(dd));
                        sum = bd1.add(bd2).doubleValue();
                    }
                    map.put("money", sum);
                }
            } else {
                if (param.getTypeName().indexOf("农村") != -1) {
                    param.setType("02");
                } else {
                    param.setType("01");
                }

                String standardMoneys = standardReadService.getStandardMoney(param.getAreaCode().substring(0,6)+"000000", Constant.BusinessCode.DIBAO, param.getType());
                if (null != standardMoneys) {
                    Double standardMoney = Double.parseDouble(standardMoneys);
                    List<SalvationRuleEntity> list = salvationRuleReadService.queryRuleCount(param);
                    IdentityHashMap<String, Double> jgMap = new IdentityHashMap<>();
                    IdentityHashMap<String, Double> pxMap = new IdentityHashMap<>();
                    if (null != list && list.size() > 0) {
                        for (SalvationRuleEntity entity : list) {
                            if (StringUtils.isNotBlank(entity.getRelationshipType()) && entity.getRelationshipType().equals("01")) {
                                Hashtable<String, Object> varValues = putValue(param);
                                if (param.getTypeName().indexOf("城市") != -1 || param.getTypeName().indexOf("致困") != -1) {
                                    varValues.put("城市标准", standardMoney);
                                } else {
                                    varValues.put("农村标准", standardMoney);
                                }
                                String str = entity.getChineseFormula();
                                //System.out.println();
                                if (str.indexOf("实物种类") < 0) {

                                    FormulaConvert cvt = new FormulaConvert();
                                    ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                                    RefObject rnt = new RefObject(0);
                                    ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                                    Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                                    for (String k : rnts.keySet()) {
                                        //System.out.println(k + "=" + rnts.get(k));
                                        jgMap.put(k, (Double) rnts.get(k));
                                    }
                                }
                            } else {
                                Hashtable<String, Object> varValues = putValue(param);
                                if (param.getTypeName().indexOf("城市") != -1) {
                                    varValues.put("城市标准", standardMoney);
                                } else {
                                    varValues.put("农村标准", standardMoney);
                                }
                                String str = entity.getChineseFormula();
                                if (str.indexOf("实物种类") < 0) {

                                    FormulaConvert cvt = new FormulaConvert();
                                    ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                                    RefObject rnt = new RefObject(0);
                                    ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);
                                    Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                                    for (String k : rnts.keySet()) {
                                        //System.out.println(k + "=" + rnts.get(k));
                                        pxMap.put(k, (Double) rnts.get(k));
                                    }
                                }
                            }
                        }
                    } else {
                        result.setCode("400");
                        result.setMsg("规则为空，请先配置规则");
                        logger.error("规则为空，请先配置规则, param={}", param);
                        return result;
                    }
                    //System.out.println(jgMap);
                    //System.out.println(pxMap);
                    if (null != jgMap && jgMap.size() > 0) {
                        List<Double> dlist = new ArrayList<>();
                        for (String k : jgMap.keySet()) {
                            if (!dlist.contains(jgMap.get(k))) {
                                dlist.add(jgMap.get(k));
                            }
                        }
                        Double max = Collections.max(dlist);
                        map.put("money", max);
                    } else if (null != pxMap && pxMap.size() > 0) {
                        List<Double> dlist = new ArrayList<>();
                        for (String k : pxMap.keySet()) {
                            dlist.add(pxMap.get(k));
                        }
                        Double sum = 0d;
                        for (Double dd : dlist) {
                            BigDecimal bd1 = new BigDecimal(Double.toString(sum));
                            BigDecimal bd2 = new BigDecimal(Double.toString(dd));
                            sum = bd1.add(bd2).doubleValue();
                        }
                        map.put("money", sum);
                    }
                } else {
                    result.setCode("300");
                    result.setMsg("标准为零，请先配置标准");
                    logger.error("标准为零, param={}", param);
                }
            }

            result.setData(map);
        } catch (Exception e1) {
            e1.printStackTrace();
            result.setCode(FailureCode.ERR_20017.getCode());
            result.setMsg(FailureCode.ERR_20017.getMessage());
        }
        return result;
    }


    public Hashtable<String, Object> putValue(FormulCountParam param) {
        Hashtable<String, Object> varValues = new Hashtable<>();
        TaDictPo dictPo = this.getDictLabelByVal(DictConstant.TempasstCountType, param.getFormulaValue());
        if(null != dictPo) {
            varValues.put("临时救助金计算方式", dictPo.getLabel());
        }

        if (StringUtils.isNotBlank(param.getKindValue())) {
            dictPo = this.getDictLabelByVal(DictConstant.PracticalityType, param.getKindValue());
            if(null != dictPo) {
                varValues.put("实物种类", dictPo.getLabel());
            }
        }

        if (StringUtils.isNotBlank(param.getPovertyValue())) {
            dictPo = this.getDictLabelByVal(DictConstant.TEMP_POVERTY_FACTORS, param.getPovertyValue());
            if(null != dictPo) {
                varValues.put("主要致贫原因", dictPo.getLabel());
            }
        }

        varValues.put("临时救助累计次数", Integer.parseInt(param.getCumulative()));
        if (StringUtils.isNotBlank(param.getAccruing())) {
            Double du = Double.parseDouble(param.getAccruing());
            Integer accruing = du.intValue();
            varValues.put("临时救助累计金额", accruing);
        }
        return varValues;
    }


    public BaseResult<List<SecurityParam>> diBaoFormulCount(FormulCountParam param) {
        BaseResult<List<SecurityParam>> result = new BaseResult<>();
        List<SecurityParam> securityList = new ArrayList<>();

        if (CommonUtil.invalidArgs(param.getAreaCode(), param.getBusinessType(), param.getTaOrgId(),
                param.getTypeName())) {
            result.setCode(FailureCode.ERR_10000.getCode());
            result.setMsg(FailureCode.ERR_10000.getMessage());
            return result;
        }
        param.setAreaCode(param.getAreaCode());
        param.setBusinessType(param.getBusinessType());
        param.setTaOrgId(param.getTaOrgId());
        param.setSaveType(Constant.SaveType.ZHB);
        try {
            if (param.getTypeName().indexOf("城市") != -1) {
                param.setType("01");
            } else {
                param.setType("02");
            }

            String standardMoneys = standardReadService.getStandardMoney(param.getAreaCode().substring(0,6)+"000000", param.getBusinessType(), param.getType());
            Double standardMoney;
            if (null == standardMoneys) {
                result.setCode("300");
                logger.error("标准为零，param:{}", param);
                result.setMsg("标准为零，请先配置标准");
                return result;
            }
            else{
                standardMoney = Double.parseDouble(standardMoneys);
            }


            if (null != param.getIncome() && param.getIncome()  >= standardMoney) {
                result.setCode("300");
                result.setMsg("家庭收入大于救助标准");
                return result;
            }

            List<SalvationRuleEntity> list = salvationRuleReadService.queryRuleCount(param);
            if (null == list || list.size() == 0) {
                result.setCode("400");
                result.setMsg("规则为空，请先配置规则");
                logger.error("规则为空，请先配置规则, param={}", param);
                return result;
            }
            for (SalvationRuleEntity entity : list) {
                SecurityParam securityParam = new SecurityParam();

                securityParam.setStandardMoney(standardMoney);
                Hashtable<String, Object> varValues = diBaoValueConvert(param);


                if (null != varValues && !CommonUtil.invalidArgs(varValues.get("保障金计算方式"))) {
                    String name = varValues.get("保障金计算方式").toString();

                    String typeName = param.getTypeName().substring(0,2) + name;
                    if (name.indexOf("档") != -1) {
                        StandardEntity salvationStandardEntiy = new StandardEntity();
                        salvationStandardEntiy.setStatus(Constant.StandardStatus.ENABLE);
                        salvationStandardEntiy.setType(typeName);
                        salvationStandardEntiy.setOrgId(param.getTaOrgId());
                        salvationStandardEntiy.setAreaCode(param.getAreaCode().substring(0,6)+"000000");
                        StandardEntity saentity = standardReadService.querySalvationStandard(salvationStandardEntiy);
                        if(null != saentity ) {
                            String saentityTypeName = saentity.getType();
                            securityParam.setTypeName(saentityTypeName);
                            securityParam.setTypeMoney(saentity.getStandardMoney());
                            saentityTypeName = saentityTypeName.substring(0,4) + "标准救助金额";
                            varValues.put(saentityTypeName, saentity.getStandardMoney());
                        }else {

                            if(!CommonUtil.invalidArgs(entity.getChineseFormula())&&entity.getChineseFormula().indexOf("档")==-1) {
                                varValues.put("家庭月总收入", 0);
                            }else {
                                result.setCode("300");
                                logger.error("标准为零，param:{}", salvationStandardEntiy);
                                result.setMsg("标准为零，请先配置标准");
                                result.setData(securityList);
                                return result;
                            }
                        }
                    }
                    if (name.indexOf("补差") != -1) {
                        varValues.put("家庭月总收入", param.getIncome());
                        varValues.put("家庭人均月收入", param.getIncome());
                    }
                }
                securityParam.setName(entity.getName());
                securityParam.setSalvationTarget(entity.getSalvationTarget());
                if (param.getTypeName().indexOf("城市") != -1) {
                    varValues.put("城市标准", standardMoney);
                } else {
                    varValues.put("农村标准", standardMoney);
                }

                String str = entity.getChineseFormula();
                if (StringUtils.isNotBlank(param.getDisabilityLevel())) {
                    FormulaConvert cvt = new FormulaConvert();
                    ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                    RefObject rnt = new RefObject(0);
                    ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                    Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                    if (null != rnts && rnts.size() > 0) {
                        for (String k : rnts.keySet()) {
                            securityParam.setMoneyName(k);
                            securityParam.setMoney(rnts.get(k).toString());
                            securityList.add(securityParam);
                        }
                    }
                } else {

                    FormulaConvert cvt = new FormulaConvert();
                    ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                    RefObject rnt = new RefObject(0);
                    ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                    Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                    for (String k : rnts.keySet()) {
                        if (k.equals("分类救助金")) {
                            SecurityParam securityParam1 = new SecurityParam();
                            securityParam1.setMoneyName(k);
                            securityParam1.setMoney(rnts.get(k).toString());
                            securityList.add(securityParam1);
                        } else if (k.equals("特殊救助金")) {
                            SecurityParam securityParam1 = new SecurityParam();
                            securityParam1.setMoneyName(k);
                            securityParam1.setMoney(rnts.get(k).toString());
                            securityList.add(securityParam1);
                        } else if (k.equals("基本救助金")) {
                            securityParam.setMoneyName(k);
                            securityParam.setMoney(rnts.get(k).toString());
                            securityParam.setSalvationTarget(entity.getSalvationTarget());
                            securityList.add(securityParam);
                        }
                    }
                }
            }

            if (null == securityList || securityList.size() == 0) {
                result.setCode("400");
                result.setMsg(String.format("根据标准(%s)计算待遇金额为空，请检查政策配置是否正确", param.getFormulaValue()));
                result.setData(securityList);
                logger.error("根据标准计算待遇金额为空, param={}", param);
                return result;
            }
            moneyRound(securityList);
            result.setData(securityList);
        } catch (Exception e1) {
            logger.error("待遇计算出错, {}", param, e1);
            result.setCode(FailureCode.ERR_20017.getCode());
            result.setMsg(e1.getMessage());
        }
        return result;
    }



    public Hashtable<String, Object> diBaoValueConvert(FormulCountParam param) {
        Hashtable<String, Object> varValues = new Hashtable<>();
        TaDictPo dictPo = this.getDictLabelByVal(DictConstant.GuaranteeType, param.getFormulaValue());
        if(null != dictPo && StringUtils.isNotEmpty(dictPo.getLabel())) {
            varValues.put("保障金计算方式", dictPo.getLabel());
        }
        else {
            throw new BizException("保障金计算方式方式为空，type={}", param.getFormulaValue());
        }

        varValues.put("标准类型", param.getTypeName());
        varValues.put("保障人口数", param.getSaveMemberNumber());
        varValues.put("残疾人数", param.getDisabilityNumber());
        varValues.put("残疾类别", param.getDisabilityType()==null?"无":param.getDisabilityType());
        varValues.put("残疾等级", param.getDisabilityLevel() == null ? "无" : param.getDisabilityLevel());
        varValues.put("年龄", Integer.parseInt(param.getAge() == null ? "0" : param.getAge()));
        varValues.put("健康状况", param.getHealth() == null ? "无" : param.getHealth());
        varValues.put("重病病种", param.getSeverediseases() == null ? "无" : param.getSeverediseases());
        varValues.put("人员分类救助类别", param.getSortSaveType() == null ? "无" : param.getSortSaveType());
        varValues.put("文化程度", param.getEducation() == null ? "无" : param.getEducation());
        varValues.put("是否在校", param.getIsAtSchool() == null ? "无" : param.getIsAtSchool());
        return varValues;
    }


    public Map<String, Object> exceptionalValue(FormulCountParam param) {
        Map<String, Object> varValues = new ConcurrentHashMap<>();
        varValues.put("特困类别", param.getTypeName());
        TaDictPo dictPo = this.getDictLabelByVal(DictConstant.SelfCareAbility, param.getOneself());
        if(null != dictPo) {
            varValues.put("生活自理能力", dictPo.getLabel());
        }


        dictPo = this.getDictLabelByVal(DictConstant.GuaranteeMode, param.getProvide());
        if(null != dictPo) {
            varValues.put("供养方式", dictPo.getLabel());
        }

        return varValues;
    }


    public BaseResult<List<SecurityParam>> exceptionalFormulCount(FormulCountParam param) {
        BaseResult<List<SecurityParam>> result = new BaseResult<>();
        List<SecurityParam> securityList = new ArrayList<>();
        if (CommonUtil.invalidArgs(param.getAreaCode(), param.getBusinessType(), param.getTaOrgId(), param.getTypeName())) {
            result.setCode(FailureCode.ERR_10000.getCode());
            result.setMsg(FailureCode.ERR_10000.getMessage());
            return result;
        }
        param.setAreaCode(param.getAreaCode());
        param.setBusinessType(param.getBusinessType());
        param.setTaOrgId(param.getTaOrgId());
        if (null == param.getDisabilityLevel() || "".equals(param.getDisabilityLevel())) {
            param.setDisabilityLevel("无");
        }
        try {
            if (param.getTypeName().indexOf("城市") != -1) {
                param.setType("03");
            } else {
                param.setType("04");
            }

            logger.info("获取救助标准：{}", param);
            String standardMoneys = standardReadService.getStandardMoney(param.getAreaCode().substring(0,6)+"000000", param.getBusinessType(), param.getType());
            logger.info("救助标准结果：{}", standardMoneys);
            if (null != standardMoneys) {
                Double standardMoney = Double.parseDouble(standardMoneys);
                List<SalvationRuleEntity> list = salvationRuleReadService.queryRuleCount(param);
                if (null != list && list.size() > 0) {
                    for (SalvationRuleEntity entity : list) {
                        SecurityParam securityParam = new SecurityParam();

                        securityParam.setStandardMoney(standardMoney);
                        Map<String, Object> varValues = exceptionalValue(param);
                        varValues.put("健康状况", param.getHealth() == null ? "无" : param.getHealth());
                        varValues.put("重病病种", param.getSeverediseases() == null ? "无" : param.getSeverediseases());
                        varValues.put("残疾等级", param.getDisabilityLevel());
                        varValues.put("年龄", Integer.parseInt(param.getAge() == null ? "0" : param.getAge()));
                        varValues.put("文化程度", param.getEducation() == null ? "无" : param.getEducation());
                        varValues.put("是否在校", param.getIsAtSchool() == null ? "无" : param.getIsAtSchool());
                        if (null != varValues && varValues.size() > 0) {
                            for (String str : varValues.keySet()) {
                                if (str.indexOf("理") != -1) {
                                    String typeName = param.getTypeName().substring(0, 2) + varValues.get(str).toString() + "标准";
                                    StandardEntity SalvationStandardEntity = new StandardEntity();
                                    SalvationStandardEntity.setStatus(Constant.StandardStatus.ENABLE);
                                    SalvationStandardEntity.setType(typeName);
                                    SalvationStandardEntity.setOrgId(param.getTaOrgId());
                                    SalvationStandardEntity.setAreaCode(param.getAreaCode().substring(0,6)+"000000");
                                    StandardEntity saentity = standardReadService.querySalvationStandard(SalvationStandardEntity);
                                    if (null != saentity) {
                                        securityParam.setTypeName(saentity.getType());
                                        securityParam.setTypeMoney(saentity.getStandardMoney());
                                        varValues.put(saentity.getType(), saentity.getStandardMoney());
                                    } else {
                                        result.setCode("300");
                                        result.setMsg("标准为零，请先配置标准");
                                        logger.error("标准为零，param:{}", SalvationStandardEntity);
                                        return result;
                                    }
                                }
                            }
                        }
                        if (param.getTypeName().indexOf("城市") != -1) {
                            varValues.put("城市特困基本生活标准", standardMoney);
                        } else {
                            varValues.put("农村特困基本生活标准", standardMoney);
                        }
                        String str = entity.getChineseFormula();
                        if (str.indexOf("生活自理能力") != -1) {
                            if (StringUtils.isNotBlank(param.getOneself())) {

                                FormulaConvert cvt = new FormulaConvert();
                                ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                                RefObject rnt = new RefObject(0);
                                ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                                Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                                if (null != rnts && rnts.size() > 0) {
                                    for (String k : rnts.keySet()) {
                                        securityParam.setMoneyName(k);
                                        securityParam.setMoney(rnts.get(k).toString());
                                    }
                                    securityList.add(securityParam);
                                }
                            }
                        } else {

                            FormulaConvert cvt = new FormulaConvert();
                            ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                            RefObject rnt = new RefObject(0);
                            ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                            Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                            if (null != rnts && rnts.size() > 0) {
                                for (String k : rnts.keySet()) {
                                    securityParam.setMoneyName(k);
                                    securityParam.setMoney(rnts.get(k).toString());
                                }
                                securityList.add(securityParam);
                            }
                        }
                    }
                    if(securityList.size() == 0) {
                        result.setCode("400");
                        result.setMsg("根据政策公式无法计算结果，请检查政策配置是否正确");
                        logger.error("根据政策公式无法计算结果, varValues={}", param);
                        logger.error("政策配置：{}", JSONObject.toJSONString(list));
                        return result;
                    }
                } else {
                    result.setCode("400");
                    result.setMsg("规则为空，请先配置规则");
                    logger.error("规则为空，请先配置规则, param={}", param);
                    return result;
                }
            } else {
                logger.error("标准为零, param={}", param);
                result.setCode("300");
                result.setMsg("标准为零，请先配置标准");
            }
            moneyRound(securityList);
            result.setData(securityList);
        } catch (Exception e1) {
            logger.error("param={}, {}", param, e1);
            result.setCode(FailureCode.ERR_20017.getCode());
            result.setMsg(e1.getMessage());
        }
        return result;
    }


    public BaseResult<List<SecurityParam>> drbFormulCount(FormulCountParam param) {
        BaseResult<List<SecurityParam>> result = new BaseResult<>();
        List<SecurityParam> securityList = new ArrayList<>();
        if (CommonUtil.invalidArgs(param.getAreaCode(), param.getBusinessType(), param.getTaOrgId(),
                param.getTypeName())) {
            result.setCode(FailureCode.ERR_10000.getCode());
            result.setMsg(FailureCode.ERR_10000.getMessage());
            return result;
        }
        param.setAreaCode(param.getAreaCode());
        param.setBusinessType(param.getBusinessType());
        param.setTaOrgId(param.getTaOrgId());
        param.setSaveType(Constant.SaveType.DRB);
        try {
            if (param.getTypeName().indexOf("城市") != -1) {
                param.setType("01");
            } else {
                param.setType("02");
            }

            String standardMoneys = standardReadService.getStandardMoney(param.getAreaCode().substring(0,6)+"000000", param.getBusinessType(), param.getType());
            Double standardMoney;
            if (null == standardMoneys) {
                result.setCode("300");
                result.setMsg("标准为零，请先配置标准");
                logger.error("标准为零, param={}", param);
                return result;
            }else{
                standardMoney = Double.parseDouble(standardMoneys);
            }

            if(null==param.getIncome()){
                result.setCode("300");
                result.setMsg("家庭收入为空");
                return result;
            }

            if ( param.getIncome() <= standardMoney || param.getIncome()> standardMoney*1.5) {
                result.setCode("300");
                result.setMsg("家庭收入没有在救助标准"+standardMoney+"的1-1.5倍之间");
                return result;
            }

            if(Constant.GuaranteeType.POLICY.equals(param.getFormulaValue())){

                List<SalvationRuleEntity> list = salvationRuleReadService.queryRuleCount(param);
                if (null == list || list.size() == 0) {
                    result.setCode("400");
                    result.setMsg("规则为空，请先配置规则");
                    logger.error("规则为空，请先配置规则, param={}", param);
                    return result;
                }
                for (SalvationRuleEntity entity : list) {
                    SecurityParam securityParam = new SecurityParam();

                    securityParam.setStandardMoney(standardMoney);
                    Hashtable<String, Object> varValues = diBaoValueConvert(param);
                    securityParam.setName(entity.getName());
                    securityParam.setSalvationTarget(entity.getSalvationTarget());
                    if (param.getTypeName().indexOf("城市") != -1) {
                        varValues.put("城市标准", standardMoney);
                    } else {
                        varValues.put("农村标准", standardMoney);
                    }
                    String str = entity.getChineseFormula();
                    if (StringUtils.isNotBlank(param.getDisabilityLevel())) {
                        FormulaConvert cvt = new FormulaConvert();
                        ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                        RefObject rnt = new RefObject(0);
                        ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                        Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                        if (null != rnts && rnts.size() > 0) {
                            for (String k : rnts.keySet()) {
                                securityParam.setMoneyName(k);
                                securityParam.setMoney(rnts.get(k).toString());
                                securityList.add(securityParam);
                            }
                        }
                    } else {

                        FormulaConvert cvt = new FormulaConvert();
                        ArrayList<String> arr = cvt.SplitWord("{" + str + "}");
                        RefObject rnt = new RefObject(0);
                        ArrayList<ISentence> sts = BlockServer.build(arr, 0, rnt);

                        Map<String, Object> rnts = BlockServer.runCode(sts, varValues);
                        for (String k : rnts.keySet()) {
                            if (k.equals("分类救助金")) {
                                SecurityParam securityParam1 = new SecurityParam();
                                securityParam1.setMoneyName(k);
                                securityParam1.setMoney(rnts.get(k).toString());
                                securityList.add(securityParam1);
                            } else if (k.equals("特殊救助金")) {
                                SecurityParam securityParam1 = new SecurityParam();
                                securityParam1.setMoneyName(k);
                                securityParam1.setMoney(rnts.get(k).toString());
                                securityList.add(securityParam1);
                            } else if (k.equals("基本救助金")) {
                                securityParam.setMoneyName(k);
                                securityParam.setMoney(rnts.get(k).toString());
                                securityParam.setSalvationTarget(entity.getSalvationTarget());
                                securityList.add(securityParam);
                            }
                        }
                    }
                }
            }else {

                SecurityParam securityParam = new SecurityParam();

                securityParam.setStandardMoney(standardMoney);
                securityParam.setName("单人保");
                securityParam.setMoneyName("基本救助金");
                securityParam.setSalvationTarget("02");
                securityParam.setMoney(String.valueOf(param.getSalaryNumber()));
                securityList.add(securityParam);
            }


            if (null == securityList || securityList.size() == 0) {
                result.setCode("400");
                result.setMsg(String.format("根据标准(%s)计算待遇金额为空，请检查政策配置是否正确", param.getFormulaValue()));
                result.setData(securityList);
                logger.error("根据标准计算待遇金额为空, param={}", param);
                return result;
            }
            moneyRound(securityList);
            result.setData(securityList);
        } catch (Exception e1) {
            logger.error("待遇计算出错, {}", param, e1);
            result.setCode(FailureCode.ERR_20017.getCode());
            result.setMsg(e1.getMessage());
        }
        return result;
    }


    private TaDictPo getDictLabelByVal(String dictType, String dictVal) {
        if(StringUtils.isEmpty(dictType) || StringUtils.isEmpty(dictVal)) {
            return null;
        }
        TaDictPo dictPo = dictionaryReadService.getCodeCache(dictType, dictVal);
        return dictPo;
    }

    public void moneyRound(List<SecurityParam> list){
        for(SecurityParam param:list){
           BigDecimal money =  new BigDecimal(param.getMoney());
           param.setMoney(money.setScale(2, RoundingMode.HALF_UP).toString());
        }
    }
}
